package cn.ctodb.divination.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.divination.service.ByslService;
import cn.ctodb.divination.web.rest.util.HeaderUtil;
import cn.ctodb.divination.service.dto.ByslDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing Bysl.
 */
@RestController
@RequestMapping("/api")
public class ByslResource {

    private final Logger log = LoggerFactory.getLogger(ByslResource.class);

    private static final String ENTITY_NAME = "bysl";
        
    private final ByslService byslService;

    public ByslResource(ByslService byslService) {
        this.byslService = byslService;
    }

    /**
     * POST  /bysls : Create a new bysl.
     *
     * @param byslDTO the byslDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new byslDTO, or with status 400 (Bad Request) if the bysl has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/bysls")
    @Timed
    public ResponseEntity<ByslDTO> createBysl(@RequestBody ByslDTO byslDTO) throws URISyntaxException {
        log.debug("REST request to save Bysl : {}", byslDTO);
        if (byslDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new bysl cannot already have an ID")).body(null);
        }
        ByslDTO result = byslService.save(byslDTO);
        return ResponseEntity.created(new URI("/api/bysls/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /bysls : Updates an existing bysl.
     *
     * @param byslDTO the byslDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated byslDTO,
     * or with status 400 (Bad Request) if the byslDTO is not valid,
     * or with status 500 (Internal Server Error) if the byslDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/bysls")
    @Timed
    public ResponseEntity<ByslDTO> updateBysl(@RequestBody ByslDTO byslDTO) throws URISyntaxException {
        log.debug("REST request to update Bysl : {}", byslDTO);
        if (byslDTO.getId() == null) {
            return createBysl(byslDTO);
        }
        ByslDTO result = byslService.save(byslDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, byslDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /bysls : get all the bysls.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of bysls in body
     */
    @GetMapping("/bysls")
    @Timed
    public List<ByslDTO> getAllBysls() {
        log.debug("REST request to get all Bysls");
        return byslService.findAll();
    }

    /**
     * GET  /bysls/:id : get the "id" bysl.
     *
     * @param id the id of the byslDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the byslDTO, or with status 404 (Not Found)
     */
    @GetMapping("/bysls/{id}")
    @Timed
    public ResponseEntity<ByslDTO> getBysl(@PathVariable Long id) {
        log.debug("REST request to get Bysl : {}", id);
        ByslDTO byslDTO = byslService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(byslDTO));
    }

    /**
     * DELETE  /bysls/:id : delete the "id" bysl.
     *
     * @param id the id of the byslDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/bysls/{id}")
    @Timed
    public ResponseEntity<Void> deleteBysl(@PathVariable Long id) {
        log.debug("REST request to delete Bysl : {}", id);
        byslService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/bysls?query=:query : search for the bysl corresponding
     * to the query.
     *
     * @param query the query of the bysl search 
     * @return the result of the search
     */
    @GetMapping("/_search/bysls")
    @Timed
    public List<ByslDTO> searchBysls(@RequestParam String query) {
        log.debug("REST request to search Bysls for query {}", query);
        return byslService.search(query);
    }


}
